﻿function spawnNotification(theBody, theIcon, theTitle) {
    var options = {
        body: theBody,
        icon: theIcon
    }
    var n = new Notification(theTitle, options);
}

(function ($) {
    "use strict";

    $(document).ready(function () {

        // Correct code; comment out for now
        //Notification.requestPermission().then(function (result) {
        //    console.log(result);
        //    spawnNotification("Sample content body", null, "My custom title");
        //});

        // This set of code is suppose to be used by Firefox
        //if (window.webkitNotifications) {
        //    // console.log('Your web browser does support notifications!');
        //    window.alert('Your web browser does support notifications!');

        //    if (window.webkitNotifications.checkPermission() == 0) {
        //        // Good to go, you can create a notification.
        //        var myNotification = window.webkitNotifications.createNotification('icon.png', 'Item Saved', 'My Application Name');
        //        myNotification.show();

        //    } else {
        //        window.webkitNotifications.requestPermission(function () { });
        //    }
        //} else {
        //    //console.log('Your web browser does not support notifications!');
        //    window.alert('Your web browser does not support notifications!');
        //}

        // SignalR Hub
        //Set the hubs URL for the connection
        console.log($.connection.hub.transport);
        $.connection.hub.logging = true;
        $.connection.hub.url = "http://localhost:80/signalr";

        var chat = $.connection.chatHub;                            // Declare a proxy to reference the hub. 
        chat.client.broadcastMessage = function (name, message) {   // Create a function that the hub can call to broadcast messages.
            // Html encode display name and message. 
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page. 
            //$('.chat-panel .chat-messages').append('<li><strong>' + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            var html = "";
            html = '<li><strong>' + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>';
            //html = html + '<div class="alert alert-message" role="alert">';
            //html = html + '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>';
            //html = html + '<span class="sr-only">Error:</span>';
            //html = html + encodedMsg;
            //html = html + '<div style="float:right">';
            //html = html + '<strong>' + encodedName + '</strong>';
            //html = html + '<br /><span>' + encodedName + '</span>';
            //html = html + '</div>';
            //html = html + '</div>';

            //$('.chat-panel .chat-messages').append('<div class="alert alert-info" role="alert"><a href="#" class="alert-link">asdasdsa</a></div>');
            $('.chat-panel .chat-messages').prepend(html);

            toastr.info(encodedMsg, encodedName);
        };

        // Get the user name and store it to prepend to messages.
        $(".chat-panel .chat-panel-username").val($(".username").text().trim());

        // Set initial focus to message input box.  
        $('.chat-panel input.message-textbox').focus();

        // Start the connection.
        $.connection.hub.start().done(function () {
            $('.chat-panel .send-button').click(function () {

                // Call the Send method on the hub. 
                chat.server.send($('.chat-panel .chat-panel-username').val(), $('input.message-textbox').val());

                // Clear text box and reset focus for next comment. 
                $('.chat-panel input.message-textbox').val('').focus();
            });
        });

        $(".chat-panel .message-textbox").keydown(function (evt) {
            if (evt.which === 13) {
                evt.preventDefault();
                $('.chat-panel .send-button').click();
            }
        });

    });

}(jQuery));